﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;

namespace Qreed.Xna.Font
{
    public abstract class VectorFontCharacterMesh : VectorFontCharacterBase
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="VectorFontCharacterMesh"/> class.
        /// </summary>
        /// <param name="font">The font.</param>
        /// <param name="character">The character.</param>
        internal VectorFontCharacterMesh(RenderFont font)
            : base(font)
        {
        }

        /// <summary>
        /// Loads the content.
        /// </summary>
        /// <param name="basedOnCharacter">The based on character.</param>
        public virtual void LoadContent(VectorFontCharacter basedOnCharacter)
        {
            Advancement = basedOnCharacter.Advancement;
            Character = basedOnCharacter.Character;
            
            BuildVertexBuffer(basedOnCharacter, out _vertices, out _indices);
        }

        /// <summary>
        /// Unloads the content.
        /// </summary>
        public virtual void UnloadContent()
        {
        }

        /// <summary>
        /// Builds the vertex buffer.
        /// </summary>
        /// <param name="copyFrom">The copy from.</param>
        /// <param name="vertices">The vertices.</param>
        /// <param name="indices">The indices.</param>
        protected abstract void BuildVertexBuffer(VectorFontCharacter copyFrom, out VertexPositionNormalTexture[] vertices, out short[] indices);

        /// <summary>
        /// Draws this instance.
        /// </summary>
        public virtual void Draw()
        {
            if (_vertices.Length > 0)
            {
                GraphicsDevice g = ((RenderFont)VectorFont).GraphicsDevice;

                g.VertexDeclaration = ((RenderFont)VectorFont).VertexDeclaration;
                g.DrawUserIndexedPrimitives<VertexPositionNormalTexture>(PrimitiveType.TriangleList, _vertices, 0, _vertices.Length,
                                                                         _indices, 0, _indices.Length / 3);
            }
        }

        private VertexPositionNormalTexture[] _vertices;
        /// <summary>
        /// Gets the vertices.
        /// </summary>
        /// <value>The vertices.</value>
        public VertexPositionNormalTexture[] Vertices
        {
            get { return this._vertices; }
        }

        private short[] _indices;
        /// <summary>
        /// Gets the indices.
        /// </summary>
        /// <value>The indices.</value>
        public short[] Indices
        {
            get { return this._indices; }
        }
    }
}
